﻿namespace ExamV5.StudyInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmEditBookInfo : Page
    {
        protected Button butClose;
        protected Button butDefault;
        protected DropDownList DDLType;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Image ImgBook;
        private int int_0 = 0;
        private PublicFun publicFun_0 = new PublicFun();
        protected TextBox txtBookName;
        protected TextBox txtFen;
        protected TextBox txtInputTime;
        protected TextBox txtMainUser;
        protected TextBox txtMemberFen;
        protected TextBox txtSortInfo;
        protected TextBox txtUnitName;
        protected HtmlInputFile UPUserData;

        protected void butDefault_Click(object sender, EventArgs e)
        {
            string strSql = "";
            string fileName = this.UPUserData.PostedFile.FileName;
            if (fileName.Trim() != "")
            {
                string str3 = fileName.Substring(fileName.Length - 4);
                string str4 = ".JPG.GIF";
                if (str4.IndexOf(str3.ToUpper()) >= 0)
                {
                    if (fileName.IndexOf(@"\", 0) > 0)
                    {
                        int num2 = fileName.LastIndexOf(@"\");
                        fileName = fileName.Substring(num2 + 1);
                        fileName = str3;
                    }
                    this.UPUserData.PostedFile.SaveAs(base.Server.MapPath("../PublicFile/") + DateTime.Now.ToString("yyyymmdd") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName);
                    fileName = DateTime.Now.ToString("yyyymmdd") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName;
                }
                else
                {
                    base.Response.Write("<script>alert('封面照片格式不正确！请上传jpg或gif图片文件')</script>");
                    return;
                }
            }
            if (this.int_0 == 0)
            {
                if (fileName.Trim() == "")
                {
                    fileName = "BookHome.jpg";
                }
                strSql = "insert into BookInfo(SubjectName,SubjectTypeID,InputUnit,InputTime,MainCheckUser,SortInfo,CreatUser,FenValue,RemoveFen,BookImage) values(";
                strSql = strSql + "'" + this.txtBookName.Text.Trim() + "'," + this.DDLType.SelectedItem.Value + ",'" + this.txtUnitName.Text.Trim() + "','" + this.txtInputTime.Text.Trim() + "','" + this.txtMainUser.Text.Trim() + "','" + this.txtSortInfo.Text.Trim() + "','" + this.httpCookie_0.Value + "',";
                strSql = strSql + "'" + this.txtFen.Text.Trim() + "','" + this.txtMemberFen.Text.Trim() + "','" + fileName + "')";
                this.publicFun_0.ExecuteSql(strSql);
            }
            else
            {
                strSql = "update BookInfo set SubjectName='" + this.txtBookName.Text.Trim() + "',SubjectTypeID=" + this.DDLType.SelectedItem.Value + ",InputUnit='" + this.txtUnitName.Text.Trim() + "',InputTime='" + this.txtInputTime.Text.Trim() + "',";
                if (fileName.Trim() != "")
                {
                    strSql = strSql + "BookImage='" + fileName + "',";
                }
                strSql = string.Concat(new object[] { strSql, "MainCheckUser='", this.txtMainUser.Text.Trim(), "',SortInfo='", this.txtSortInfo.Text.Trim(), "',FenValue='", this.txtFen.Text.Trim(), "',RemoveFen='", this.txtMemberFen.Text.Trim(), "',CreatUser='", this.httpCookie_0.Value, "' where ID=", this.int_0 });
                this.publicFun_0.ExecuteSql(strSql);
            }
            base.Response.Write("<script>alert('保存教材信息操作成功！');parent.location.reload();WindowClosed();</script>");
        }

        private void method_0()
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select * from BookInfo where ID=" + this.int_0, connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                this.DDLType.SelectedIndex = -1;
                this.txtBookName.Text = reader["SubjectName"].ToString();
                this.txtUnitName.Text = reader["InputUnit"].ToString();
                this.txtMainUser.Text = reader["MainCheckUser"].ToString();
                this.txtInputTime.Text = reader["InputTime"].ToString();
                this.txtFen.Text = reader["FenValue"].ToString();
                this.txtMemberFen.Text = reader["RemoveFen"].ToString();
                this.DDLType.Items.FindByValue(reader["SubjectTypeID"].ToString()).Selected = true;
                this.txtSortInfo.Text = reader["SortInfo"].ToString();
                this.ImgBook.ImageUrl = "../PublicFile/" + reader["BookImage"].ToString();
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                base.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString + "---编辑电子教材";
                this.int_0 = Convert.ToInt32(base.Request["ID"].ToString());
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) == 0x3a)
                {
                    base.Response.Write("<script>alert('该版本为特惠版，此功能未开放！')</script>");
                }
                else if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        base.Response.Write("<script>alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')</script>");
                    }
                    else
                    {
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddText,ManagText from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddPower"] = reader["AddText"].ToString();
                            this.ViewState["ManagPower"] = reader["ManagText"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        if (!((this.ViewState["AddPower"].ToString() != "Y") ? !(this.ViewState["ManagPower"].ToString() != "Y") : true))
                        {
                            base.Response.Write("<script>alert('添加或管理电子教材权限未开放！')</script>");
                            this.butDefault.Visible = false;
                        }
                        else
                        {
                            if (!(this.ViewState["ManagPower"].ToString() == "Y"))
                            {
                                this.publicFun_0.GetType(this.DDLType, "BookType", this.httpCookie_0.Value, "N", this.httpCookie_3.Value);
                            }
                            else
                            {
                                this.publicFun_0.GetType(this.DDLType, "BookType", this.httpCookie_0.Value, "Y", this.httpCookie_3.Value);
                            }
                            ListItem item = new ListItem("--请选择教材类别--", "0");
                            this.DDLType.Items.Insert(0, item);
                            this.DDLType.SelectedIndex = -1;
                            this.DDLType.Items.FindByValue("0").Selected = true;
                            if (this.int_0 != 0)
                            {
                                this.method_0();
                            }
                        }
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

